import { Controller } from "../../mvc/core/Controller";
import { IIntegralShopController } from "./interfaces/IIntegralShopController";
import { IntegralShopIcon } from "../view/integralShop/IntegralShopIcon";
import { IView } from "../../mvc/interfaces/IView";
import { IntegralShopModule } from "../../../modules/IntegralShopModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SIntegralShopInfo } from "../../../Message/Public/SIntegralShopInfo";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { EPrictUnit } from "../../../Message/Public/EPrictUnit";
import { ECareer } from "../../../Message/Public/ECareer";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { SMoney } from "../../../Message/Game/SMoney";
import { SIntegralExchangeRecords } from "../../../Message/Public/SIntegralExchangeRecords";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { IIntegralShopModule } from "../../../modules/interfaces/IIntegralShopModule";
type int = number;
//class IntegralShopController
    
    export  class IntegralShopController extends Controller implements IIntegralShopController
    {
       

        public get integralShopIcon(): IntegralShopIcon
        {
            if (this._integralShopIcon == null) 
            {
                this._integralShopIcon = new IntegralShopIcon();
                this._integralShopIcon.updateIconInfo();
            }
            return this._integralShopIcon;
        }

        protected /* override */ initView(): IView
        {
            if (this._integralShop == null) 
            {
                this._integralShop = new IntegralShopModule();
            }
            return this._integralShop;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicIntegralShopInfo, this.onIntegralShopInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.IntegralShopActive, this.onIntegralShopActiveHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.IntegralExchangeRecordsCommand, this.onIntegralExchangeRecordsCommandHandler);
            Dispatcher.addEventListener(EventName.IntegralShopModuleOpen, this.onModuleOpenHandler);
            Dispatcher.addEventListener(EventName.IntegralShopIsOpen, this.onIntegralShopIsOpenHandler);
            Dispatcher.addEventListener(EventName.IntegralShopBuyItem, this.onBuyItemHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            return;
        }

        /* internal  */onIntegralShopInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SIntegralShopInfo;
            if (this._integralShop && !this._integralShop.isHide) 
            {
                this._integralShop.updateShopList(loc1);
            }
            return;
        }

        /* internal  */onIntegralShopActiveHandler(arg1: Object): void
        {
            var loc1=arg1.activeToClient as SActiveToClient;
            var loc2=arg1.type as int;
            cache.integralExchange.setOpenType(loc2);
            cache.integralExchange.activeToClient = loc1;
            var loc3=cache.active.isActiveInEffect(loc1.code);
            var loc4=cache.role.entityInfo.level;
            var loc5=cache.role.entityInfo.career;
            var loc6=cache.integralExchange.getIntegralTabDataArray(EPrictUnit._EPriceUnitGold, EPrictUnit._EPriceUnitGold, EPrictUnit._EPriceUnitGold);
            if (loc3 && cache.role.getisHideExploreShop && loc4 >= 12 && !(loc5 == ECareer._ECareerNewPlayer) && !(loc5 == ECareer._ECareerNo) && loc6 && loc6.length > 0) 
            {
                this.integralShopIcon.show();
            }
            return;
        }

        /* internal  */onActiveListChangeHandler(arg1: Object): void
        {
            var loc1=cache.active.isActiveInEffect(EActiveType._EActiveTypeIntegralShopOne);
            var loc2=cache.active.isActiveInEffect(EActiveType._EActiveTypeIntegralShopTwo);
            if (!loc1 && !loc2) 
            {
                if (this._integralShopIcon && !this._integralShopIcon.isHide) 
                {
                    this._integralShopIcon.hide();
                }
            }
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: SMoney): void
        {
            if (this._integralShop && !this._integralShop.isHide) 
            {
                this._integralShop.updateMoney(arg1);
            }
            return;
        }

        /* internal  */onIntegralExchangeRecordsCommandHandler(arg1: SIntegralExchangeRecords): void
        {
            if (this._integralShop && !this._integralShop.isHide) 
            {
                this._integralShop.updateRecord(arg1);
            }
            return;
        }

        /* internal  */onModuleOpenHandler(arg1: DataEvent): void
        {
            var loc2=0;
            var loc1=cache.integralExchange.getIntegralTabDataArray(EPrictUnit._EPriceUnitGold, EPrictUnit._EPriceUnitGold, EPrictUnit._EPriceUnitGold);
            if (loc1 && loc1.length <= 0) 
            {
                return;
            }
            if (view.isHide) 
            {
                view.show();
            }
            else 
            {
                view.hide();
            }
            if (!(arg1.data == null) && this._integralShop) 
            {
                loc2 = arg1.data as int;
                this._integralShop.updateShopByIndex(loc2);
            }
            return;
        }

        /* internal  */onIntegralShopIsOpenHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            var loc2=arg1.isResult;
            GameProxy.shopProxy.openIntegralShop(loc2, loc1);
            return;
        }

        /* internal  */onBuyItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.shopProxy.buyIntegralShopItem(loc1.shopType, loc1.itemCode, loc1.amount);
            return;
        }

        /* internal  */onRoleLevelUpdateHandler(arg1: Object): void
        {
            if (this._integralShop && !this._integralShop.isHide) 
            {
                this._integralShop.updateTabBar();
            }
            return;
        }

        private /* var */_integralShop: IIntegralShopModule;

        private /* var */_integralShopIcon: IntegralShopIcon;
    }
